<?php
    class DBObject
    {
        public static function getManufacturers()
        {
            $oDatabase = new database();
            $squery = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink("id/bussiness#").'>

            SELECT * 
            WHERE
            {
            ?uri rdf:type bus:manufacturer.
            ?uri bus:name ?name. 
            }';
            $aManufacturers = $oDatabase->loadQuery($squery);   
            return $aManufacturers['result']['rows'];
        }

        public static function getUsers()
        {
            $oDatabase = new database();
            $squery = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX bus: <'.URL::getLink("id/bussiness#").'>

            SELECT * 
            WHERE
            {
            ?uri rdf:type foaf:Person.
            ?uri foaf:familyName ?name. 
            ?uri foaf:givenName ?givenName. 
            ?uri foaf:mbox ?email. 
            ?uri bus:location ?location. 
            OPTIONAL{?uri bus:image ?image. }
            }';
            $aManufacturers = $oDatabase->loadQuery($squery);   
            return $aManufacturers['result']['rows'];
        }

        public static function getLocations()
        {
            $oDatabase = new database();
            $sQuery = 'SELECT * FROM location ';
            $aLocations = $oDatabase->getListObject($sQuery,true,true);
            return $aLocations;
        }

        public static function getProviders()
        {
            $oDatabase = new database();
            $sQuery = 'SELECT * FROM provider ';
            $aLocations = $oDatabase->getListObject($sQuery,true,true);
            return $aLocations;
        }

        public static function getProducers()
        {
            $oDatabase = new database();
            $sQuery = 'SELECT * FROM producers ';
            $aLocations = $oDatabase->getListObject($sQuery,true,true);
            return $aLocations;
        }

        public static function addProduct($sUserUri,$sProductName,$sProductPrice,$sProductDescription,$aImages,$sCategoryUri,$aCategoriesUri,$sManufacturerUri,$sLocation)
        {
            $oDatabase = new database();
            $lastProduct = $oDatabase->getObject('SELECT * FROM settingid WHERE tablename = "product"');
            $product_id = core::getID($lastProduct->lastest_id,'product');
            core::updateSettingID('product',((int)$lastProduct->lastest_id+1));
            $sType = 'product';
            $aNameSpace = array(
                'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                'bus' =>  URL::getLink('id/bussiness#')
            );
            $uri = URL::getLink('product/id/'.$product_id);
            $data = array(
                $uri => array(
                    "rdf:type" => array( 
                        array( "value" => URL::getLink("id/bussiness#product"), 
                            "type" => "uri" ) 
                    ) ,
                    "bus:name" => array(
                        array( "value" => $sProductName, 
                            "type" => "literal")
                    ),
                    "bus:price" => array(
                        array( "value" => $sProductPrice, 
                            "type" => "literal")
                    ),
                    "bus:description" => array(
                        array( "value" => $sProductDescription, 
                            "type" => "literal")
                    ),
                    "bus:defaultImage" => array(
                        array( "value" => URL::getLink('upload/'.$aImages[0]), 
                            "type" => "literal")
                    ),
                    "bus:hasCategory" => array(
                        array( "value" => $sCategoryUri, 
                            "type" => "uri")
                    ),
                    "bus:hasManufacturer" => array(
                        array( "value" => $sManufacturerUri, 
                            "type" => "uri")
                    ),
                    "bus:ownUser" => array(
                        array( "value" => $sUserUri, 
                            "type" => "uri")
                    ),
                    "bus:location" => array(
                        array( "value" => $sLocation, 
                            "type" => "literal")
                    ),
                    "bus:publishDate" => array(
                        array( "value" => date('d'), 
                            "type" => "literal")
                    ),
                    "bus:publishTime" => array(
                        array( "value" => time(), 
                            "type" => "literal")
                    ),
                    "bus:publishMonth" => array(
                        array( "value" => date('m'), 
                            "type" => "literal")
                    ),
                    "bus:publishYear" => array(
                        array( "value" => date('Y'), 
                            "type" => "literal")
                    )
                ) 
            );
            // Add image into array data
            $aImageTemps = array();
            foreach($aImages as $img)
            {
                $aTemp = array( "value" => URL::getLink('upload/'.$img) ,"type" => "literal");
                array_push($aImageTemps , $aTemp);
            }
            $data[$uri]['bus:image'] = $aImageTemps;
            // split sParentCategory into array Categories
            $str = chop($aCategoriesUri);
            $sCategories = explode(';', $str);
            $aCategories = array();
            foreach($sCategories as $sCategory)
            {
                $aTemp = array( "value" => $sCategory ,"type" => "uri");
                array_push($aCategories , $aTemp);
            }
            $data[$uri]['bus:childCategoryOf'] = $aCategories;
            $oDatabase ->createResources($aNameSpace,$data,$product_id,$sType);
            return $product_id;
        }

        public static function updataProduct($uri,$sProductName,$sProductPrice,$sProductDescription,$aImages,$sCategoryUri,$aCategoriesUri,$sManufacturerUri,$sLocation)
        {
            $graph = new Graphite();
            $graph->ns('bus',URL::getLink('id/bussiness#'));
            $graph->setDebug( true );
            $graph->load($uri);  
            $graph->resource($uri)->set('bus:name',$sProductName);
            $graph->resource($uri)->set('bus:price',$sProductPrice);
            $graph->resource($uri)->set('bus:description',$sProductDescription);
            $graph->resource($uri)->set('bus:location',$sLocation);
            $graph->resource($uri)->set('bus:hasManufacturer',$sManufacturerUri);
            $graph->resource($uri)->set('bus:hasCategory',$sCategoryUri);
            if(count($aImages))
            {
                $graph->resource($uri)->set('bus:defaultImage',URL::getLink('upload/'.$aImages[0]));
                foreach($aImages as $image)
                {
                    $graph->resource($uri)->add('bus:image',URL::getLink('upload/'.$image));
                }
            }
            $str = chop($aCategoriesUri);
            $sCategories = explode(';', $str);
            if(count($sCategories))
            {
                $graph->resource($uri)->remove("bus:childCategoryOf");
                foreach($sCategories as $sCategory)
                {
                    $graph->resource($uri)->add('bus:childCategoryOf',$sCategory);
                }
            }
            $doc = $graph->serialize();

            $aParams = explode('/',$uri);
            $sFile = 'data/'.end($aParams).'.rdf';
            $wfile = @fopen(trim($sFile),"w");
            fwrite($wfile,$doc);
            fclose($wfile);
            if($wfile)
            {
                // local store
                $oDatabase = new database();
                $oDatabase->removeAllProperity($uri);
                $sQuery = "DELETE FROM <".$uri.">";
                $result = $oDatabase->loadQuery($sQuery);
                $result = $oDatabase->loadQuery("LOAD <$uri>");
                return $result;
            }
            else
            {
                return false;
            }  
        }
    }
?>
